package com.zahb.qadx.ui.activity.topic;

import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.Editable;
import android.text.Html;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.PagerSnapHelper;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;

import com.githang.statusbar.StatusBarCompat;
import com.google.gson.Gson;
import com.luck.picture.lib.tools.ToastUtils;
import com.zahb.qadx.R;
import com.zahb.qadx.base.BaseActivity;

import com.zahb.qadx.model.AnswerBean;
import com.zahb.qadx.model.SpecialBean;
import com.zahb.qadx.model.SpecialSingleTopicBean;
import com.zahb.qadx.model.SpecialSubmitted;
import com.zahb.qadx.net.RetrofitService;
import com.zahb.qadx.ui.view.MyDialog;
import com.zahb.qadx.util.Constant;
import com.zahb.qadx.util.Tips;
import org.jetbrains.annotations.NotNull;
import java.util.ArrayList;
import java.util.List;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import okhttp3.MediaType;
import okhttp3.RequestBody;

//专项练习
public class SpecialPracticeActivity extends BaseActivity implements OnItemClickListener {
    @BindView(R.id.current_qid)
    TextView currentQid;
    @BindView(R.id.the_topic_order)
    TextView theTopicOrder;
    @BindView(R.id.first_layout)
    LinearLayout firstLayout;
    @BindView(R.id.recycler_view)
    RecyclerView mRecyclerView;
    @BindView(R.id.sheet)
    TextView sheet;
    @BindView(R.id.on_a_topic)
    TextView onATopic;
    @BindView(R.id.next_question)
    TextView nextQuestion;
    private int mId;
    // 答题卡
    private MyDialog mMyDialog;
    public RecyclerView answer_sheetr_ecyclerView;
    private SpecialBean mSpecialBean = new SpecialBean();
    private BaseQuickAdapter<SpecialBean.AnswerListBean, BaseViewHolder> mAdapter;
    private String[] serialNumber = {"A", "B", "C", "D", "E", "F", "G", "H"};
    private String[] theSerialNumber = {"(1) . ", "(2) . ", "(3) . ", "(4) . ", "(5) . ", "(6) . ", "(7) . ", "(8) . ", "(9) . ", "(10) . "};
    public LinearLayoutManager mLayoutManager;
    private BaseQuickAdapter<SpecialBean.AnswerListBean, BaseViewHolder> mOutsideAdapter;
    String questionType = "";
    public BaseQuickAdapter<SpecialSingleTopicBean.OptionListBean, BaseViewHolder> OptionListBeanAdapter;
    public String refer_to_the_answer = "";
    public List<AnswerBean> mShortanSwerBeanList;
    private int ForThefirstTime = 0;
    private int record = 1;
    private int repeat = -1;

    private SpecialSingleTopicBean specialSingleTopicBean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        navigationInitialization();
        mUnbinder = ButterKnife.bind(this);
        mId = getIntent().getExtras().getInt("id");
        specialQid();//获取题号
    }

    private void specialQid() {
        addDisposable(RetrofitService.getNetService()
                .getQidSpecial(mId)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(response -> {
                    if (response.getStatusCode() == Constant.CODE_SUCCESS) {
                        mSpecialBean = response.getData();

                        processingUnifiedData(mSpecialBean.getAnswerList());
                    } else {

                    }
                }, throwable -> {
                    throwable.printStackTrace();
                    Tips.RequestError(getActivity());
                }));
    }

    //答题卡
    private void answerSheetList() {
//        mOutsideAdapter.notifyDataSetChanged();
        View view = getLayoutInflater().inflate(R.layout.answer_sheet_layout, null);
        mMyDialog = new MyDialog(getActivity(), 0, 0, view, R.style.dialog);
        TextView answer_cancel = view.findViewById(R.id.answer_cancel);
        answer_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mMyDialog.dismiss();
            }
        });
        answer_sheetr_ecyclerView = view.findViewById(R.id.answer_sheetr_ecyclerView);
        answer_sheetr_ecyclerView.setLayoutManager(new GridLayoutManager(this, 5));
        answer_sheetr_ecyclerView.addItemDecoration(new RecyclerView.ItemDecoration() {
            @Override
            public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
                outRect.set(0, 0, 0, 0);
            }
        });
        mAdapter = new BaseQuickAdapter<SpecialBean.AnswerListBean, BaseViewHolder>(R.layout.team_members_layout) {
            @Override
            protected void convert(@NotNull BaseViewHolder baseViewHolder, SpecialBean.AnswerListBean itemsBean) {
                TextView text_content = baseViewHolder.findView(R.id.text_content);
                text_content.setText(itemsBean.getIndex() + "");
                if (itemsBean.getIsRight() == 0) {
                    text_content.setBackground(getContext().getResources().getDrawable(R.drawable.grey_circle_360dp));
                } else if (itemsBean.getIsRight() == 1) {
                    text_content.setBackground(getContext().getResources().getDrawable(R.drawable.the_green_circle));
                } else {
                    text_content.setBackground(getContext().getResources().getDrawable(R.drawable.the_red_circle_360dp));
                }
                text_content.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        mRecyclerView.scrollToPosition(baseViewHolder.getAdapterPosition());


                        controlTheAnswer(mSpecialBean.getAnswerList().size(), baseViewHolder.getAdapterPosition() + 1,1);
                        mMyDialog.dismiss();
                    }
                });

            }
        };
        mAdapter.addData(mSpecialBean.getAnswerList());
        answer_sheetr_ecyclerView.setAdapter(mAdapter);
    }

    //导航栏初始化
    private void navigationInitialization() {
        StatusBarCompat.setStatusBarColor(this, getResources().getColor(R.color.blue));//状态栏颜色
        mTopBarContainer.setBackgroundColor(getResources().getColor(R.color.blue));//头部布局颜色
        mTitle_view.setTextColor(getResources().getColor(R.color.white));//标题颜色
        Drawable drawable = getResources().getDrawable(R.mipmap.fanhui);//返回箭头
        drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());// 设置边界
        // param 左上右下
        mTopBarStartText.setCompoundDrawables(drawable, null, null, null);
        mTopBarStartText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }

    @Override
    protected int getLayout() {
        return R.layout.activity_special_practice;
    }

    @Override
    protected void initViews() {
    }
    @Override
    protected int getTitleStringRes() {
        return R.string.special_practice;
    }

    @OnClick({R.id.sheet, R.id.on_a_topic, R.id.next_question})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.sheet:
                answerSheetList();
                mMyDialog.show_widescreen();
                answer_sheetr_ecyclerView.scrollToPosition(mLayoutManager.findFirstCompletelyVisibleItemPosition());
                break;
            case R.id.on_a_topic:

                if (mLayoutManager.findFirstCompletelyVisibleItemPosition() == 0) {
                    ToastUtils.s(this, "当前是第一道题");
                } else {
                    if (mLayoutManager.findLastCompletelyVisibleItemPosition() == -1) return;
                    controlTheAnswer(mSpecialBean.getAnswerList().size(), mLayoutManager.findFirstCompletelyVisibleItemPosition(),1);
                }
                nextQuestion.setText("下一题");
                break;
            case R.id.next_question:
                if (mLayoutManager.findFirstCompletelyVisibleItemPosition() == (mSpecialBean.getAnswerList().size() - 2)) {
                    nextQuestion.setText("完成");
                }
                if (mLayoutManager.findFirstCompletelyVisibleItemPosition() == (mSpecialBean.getAnswerList().size() - 1)) {
                    finish();
                    ToastUtils.s(this, "已提交");
                } else {

                    if (mLayoutManager.findLastCompletelyVisibleItemPosition() == -1) return;
                    controlTheAnswer(mSpecialBean.getAnswerList().size(), mLayoutManager.findFirstCompletelyVisibleItemPosition() + 2,1);

                }
                break;
        }
    }
    private void controlTheAnswer(int allAnswer, int current ,int address) {
        hideInput();
        if (address==2){
            Log.e("yemian",current+"");
            mOutsideAdapter.notifyItemChanged(record-1);
            record = current;
        }

        currentQid.setText(current + "");
        if (current == allAnswer) {
            nextQuestion.setText("完成");
        } else {
            nextQuestion.setText("下一题");
        }

        if (current ==1){
            onATopic.setVisibility(View.INVISIBLE);
        }else {
            onATopic.setVisibility(View.VISIBLE);
        }
        if (mLayoutManager.findFirstCompletelyVisibleItemPosition() - current > 5 || current - mLayoutManager.findFirstCompletelyVisibleItemPosition() > 5) {
            mRecyclerView.scrollToPosition(current - 1);
            theTopicOrder.setText("/" + mSpecialBean.getAnswerList().size() + "   " + testingTypes(mSpecialBean.getAnswerList().get(current - 1).getQuestionType()));
            Log.e("yemian",current+"");
            mOutsideAdapter.notifyItemChanged(record-1);
            record = current;
        } else {
                mRecyclerView.smoothScrollToPosition(current - 1);
                theTopicOrder.setText("/" + mSpecialBean.getAnswerList().size() + "   " + testingTypes(mSpecialBean.getAnswerList().get(current - 1).getQuestionType()));

        }
    }
    /**
     * 隐藏键盘
     */
    protected void hideInput() {
        InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
        View v = getWindow().peekDecorView();
        if (null != v) {
            imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
        }

    }
    private String testingTypes(int questionType) {
        switch (questionType) {
            case 1:
                return "单选题";
            case 2:
                return "多选题";
            case 3:
                return "判断题";
            case 4:
                return "填空题";
            case 5:
                return "简答题";
            case 6:
                return "案例题";

        }
        return "试题";
    }

    private void processingUnifiedData(List<SpecialBean.AnswerListBean> mAnswerListBean) {

        mLayoutManager = new LinearLayoutManager(this);
        mLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        mRecyclerView.setLayoutManager(mLayoutManager);
        //创建适配器
        //给RecyclerView设置适配器
        mRecyclerView.setAdapter(mOutsideAdapter);
        PagerSnapHelper snapHelper = new PagerSnapHelper();
        //加在setAdapter后面
        snapHelper.attachToRecyclerView(mRecyclerView);


        mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);

                if (newState == 0 && mLayoutManager.findFirstCompletelyVisibleItemPosition() != -1) {
                    if (repeat!=mLayoutManager.findFirstCompletelyVisibleItemPosition()){
                        Log.e("当前位置", newState + "++++" + mLayoutManager.findFirstCompletelyVisibleItemPosition());
                        controlTheAnswer(mSpecialBean.getAnswerList().size(), mLayoutManager.findFirstCompletelyVisibleItemPosition() + 1,2);
                        repeat=mLayoutManager.findFirstCompletelyVisibleItemPosition();
                    }else {

                    }

                }
            }
        });
        mOutsideAdapter = new BaseQuickAdapter<SpecialBean.AnswerListBean, BaseViewHolder>(R.layout.special_layout) {
            @Override
            protected void convert(@NotNull BaseViewHolder baseViewHolder, SpecialBean.AnswerListBean itemsBean) {
                getTheData(baseViewHolder, itemsBean);

            }
        };
        mOutsideAdapter.addData(mAnswerListBean);
        mRecyclerView.setAdapter(mOutsideAdapter);
        int j=0;
        for (int i = 0; i <mAnswerListBean.size() ; i++) {
            if (mAnswerListBean.get(i).getIsRight()!=0){//判断最后答题位置
                j=i;
            }

        }
        //跳到上次答题位置 继续答题
        controlTheAnswer(mSpecialBean.getAnswerList().size(), j + 1,1);
    }

    private void getTheData(BaseViewHolder baseViewHolder, SpecialBean.AnswerListBean itemsBean) {
        RecyclerView  recyclerView = baseViewHolder.findView(R.id.answer_list);// 单选 多选 判断
        WebView webView = baseViewHolder.findView(R.id.content_text); //题干
        TextView no_labels=baseViewHolder.findView(R.id.no_labels);
        TextView correct_answer = baseViewHolder.findView(R.id.correct_answer);//参考答案
        TextView parsing = baseViewHolder.findView(R.id.parsing);//解析
        EditText short_answer_text = baseViewHolder.findView(R.id.short_answer_text);//简答 案例
        LinearLayout the_analysis_of = baseViewHolder.findView(R.id.the_analysis_of);//答案布局
        TextView answer = baseViewHolder.findView(R.id.answer);//选择答案
        TextView more_determine = baseViewHolder.findView(R.id.more_determine);//提交
        webView.setBackgroundColor(0);//背景透明
        TextView check_the_parsing = baseViewHolder.findView(R.id.check_the_parsing);//解析
        addDisposable(RetrofitService.getNetService()
                .getASingleItem(itemsBean.getId() + "")
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(response -> {
                    if (response.getStatusCode() == Constant.CODE_SUCCESS) {
                        specialSingleTopicBean = new SpecialSingleTopicBean();
                        specialSingleTopicBean = response.getData();

                        itemsBean.setQuestionType(specialSingleTopicBean.getQuestionType());
                        if (ForThefirstTime == 0) {
                            theTopicOrder.setText("/" + mSpecialBean.getAnswerList().size() + "   " + testingTypes(specialSingleTopicBean.getQuestionType()));
                            ForThefirstTime = 1;
                        }
                        if (theTopicOrder.getText().toString().indexOf("试题") != -1) {
                            theTopicOrder.setText("/" + mSpecialBean.getAnswerList().size() + "   " + testingTypes(specialSingleTopicBean.getQuestionType()));

                        }
                        String reference = "";
                        if (specialSingleTopicBean.getQuestionType() == 4) {
                            for (int i = 0; i < specialSingleTopicBean.getOptionList().size(); i++) {
                                if (specialSingleTopicBean.getOptionList().get(i).getIsRightAnswer() == 1) {
                                    reference = reference + (theSerialNumber[i] + specialSingleTopicBean.getOptionList().get(i).getContent());
                                }
                            }
                        } else {
                            for (int i = 0; i < specialSingleTopicBean.getOptionList().size(); i++) {
                                if (specialSingleTopicBean.getOptionList().get(i).getIsRightAnswer() == 1) {
                                    reference = reference + serialNumber[i];
                                }
                            }
                        }
                        correct_answer.setText(reference);//参考答案
                        if (itemsBean.getIsRight() != 0) {//选择结果颜色 和布局显示
                            the_analysis_of.setVisibility(View.VISIBLE);
                            if (itemsBean.getIsRight() == 1) {
                                answer.setTextColor(getResources().getColor(R.color.blue));
                            } else {
                                answer.setTextColor(getResources().getColor(R.color.red_ff4500));
                            }
                        } else {
                            the_analysis_of.setVisibility(View.GONE);
                        }
                        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
                        switch (specialSingleTopicBean.getQuestionType()) {
                            case 1://单选
                                short_answer_text.setVisibility(View.GONE);
                                recyclerView.setVisibility(View.VISIBLE);
                                voidradio(itemsBean, answer, the_analysis_of, check_the_parsing, specialSingleTopicBean, recyclerView);
                                break;
                            case 2://多选
                                short_answer_text.setVisibility(View.GONE);
                                recyclerView.setVisibility(View.VISIBLE);
                                refer_to_the_answer = "";
                                multiSelect(itemsBean, answer, the_analysis_of, more_determine, specialSingleTopicBean ,recyclerView ,check_the_parsing);

                                break;
                            case 3://判断
                                short_answer_text.setVisibility(View.GONE);
                                recyclerView.setVisibility(View.VISIBLE);
                                voidradio(itemsBean, answer, the_analysis_of, check_the_parsing, specialSingleTopicBean, recyclerView);
                                break;
                            case 4://填空
                                short_answer_text.setVisibility(View.GONE);
                                recyclerView.setVisibility(View.VISIBLE);

                                fillsUpTheTopic(itemsBean, check_the_parsing, the_analysis_of, specialSingleTopicBean, answer, correct_answer);
                                OptionListBeanAdapter.addData(specialSingleTopicBean.getOptionList());
                                OptionListBeanAdapter.setOnItemClickListener(this);
                                recyclerView.setAdapter(OptionListBeanAdapter);
                                break;
                            case 5://简答
                                short_answer_text.setVisibility(View.VISIBLE);
                                recyclerView.setVisibility(View.GONE);
                                shortAnswer(short_answer_text, itemsBean, check_the_parsing, the_analysis_of, answer, correct_answer, response.getData());
                                break;
                            case 6://案例
                                short_answer_text.setVisibility(View.VISIBLE);
                                recyclerView.setVisibility(View.GONE);
                                break;

                        }
                        parsing.setText("本题解析:"+Html.fromHtml(response.getData().getParsing()).toString().trim());
                        if (response.getData().getContent().indexOf("<") != -1){
                            webView.setVisibility(View.VISIBLE);
                            no_labels.setVisibility(View.GONE);
                            jsSupport(webView, response.getData().getContent());
                        }else {
                            webView.setVisibility(View.GONE);
                            no_labels.setVisibility(View.VISIBLE);
                            no_labels.setText(Html.fromHtml(response.getData().getContent()).toString().trim());

                        }


                    } else {

                    }
                }, throwable -> {
                    throwable.printStackTrace();
                    Tips.RequestError(getActivity());
                }));


    }

    //单选 与判断
    public void voidradio(SpecialBean.AnswerListBean itemsBean, TextView answer, LinearLayout the_analysis_of, TextView check_the_parsing, SpecialSingleTopicBean specialSingleTopicBean,
                          RecyclerView recyclerView) {
        final int[] choose = {0};
        OptionListBeanAdapter = new BaseQuickAdapter<SpecialSingleTopicBean.OptionListBean, BaseViewHolder>(R.layout.choose) {
            @Override
            protected void convert(@NotNull BaseViewHolder baseViewHolder, SpecialSingleTopicBean.OptionListBean questionOptionListBean) {
                TextView topic_selection = baseViewHolder.findView(R.id.topic_selection);
                ImageView selected = baseViewHolder.findView(R.id.selected);
                TextView the_order = baseViewHolder.findView(R.id.the_order);
                final LinearLayout linearLayout = baseViewHolder.findView(R.id.the_whole_article);
                the_order.setText(questionOptionListBean.getSerialNumber()[baseViewHolder.getAdapterPosition()]);//顺序ABCD
                topic_selection.setText(Html.fromHtml(questionOptionListBean.getContent()).toString().trim());
                if (itemsBean.getIsRight() != 0) {
                    for (int i = 0; i < itemsBean.getAnswer().size(); i++) {
                        if (itemsBean.getAnswer().get(i).getId() == questionOptionListBean.getOptionId()) {
                            topic_selection.setTextColor(getResources().getColor(R.color.blue));
                            the_order.setTextColor(getResources().getColor(R.color.blue));
                            linearLayout.setBackground(getContext().getResources().getDrawable(R.drawable.asingleframe));
                            selected.setVisibility(View.VISIBLE);
                            answer.setText(serialNumber[baseViewHolder.getAdapterPosition()]);
                        } else {
                            topic_selection.setTextColor(getResources().getColor(R.color.colorBlack));
                            the_order.setTextColor(getResources().getColor(R.color.colorBlack));
                            linearLayout.setBackground(getContext().getResources().getDrawable(R.drawable.grey_circle));
                            selected.setVisibility(View.INVISIBLE);
                        }
                    }
                    the_analysis_of.setVisibility(View.VISIBLE);
                } else {

                }

                if (choose[0] == 2 || itemsBean.getIsRight() == 0) {
                    check_the_parsing.setVisibility(View.VISIBLE);
                    the_analysis_of.setVisibility(View.GONE);
                } else {
                    the_analysis_of.setVisibility(View.VISIBLE);
                    check_the_parsing.setVisibility(View.GONE);
                }

                //
                check_the_parsing.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if (itemsBean.getIsRight() != 0) {
                            choose[0] = 0;
                            recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
                            OptionListBeanAdapter.notifyDataSetChanged();
                        }else {

                            showBindToast("请选择答案");
                        }

                    }
                });
                linearLayout.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if (check_the_parsing.getVisibility()==View.VISIBLE){
                        choose[0] = 2;
                        answer.setText(serialNumber[baseViewHolder.getAdapterPosition()]);
                        if (questionOptionListBean.getIsRightAnswer() == 1) {
                            itemsBean.setIsRight(1);
                            answer.setTextColor(getResources().getColor(R.color.blue));
                        } else {
                            itemsBean.setIsRight(2);
                            answer.setTextColor(getResources().getColor(R.color.red_ff4500));
                        }
                        the_analysis_of.setVisibility(View.VISIBLE);
                        AnswerBean answerBean = new AnswerBean();
                        answerBean.setContent(questionOptionListBean.getContent());
                        answerBean.setId(questionOptionListBean.getOptionId());
                        List<AnswerBean> answerBeanList = new ArrayList<>();
                        answerBeanList.add(answerBean);
                        itemsBean.setAnswer(answerBeanList);
                        answerToSubmit(itemsBean);
                        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
                        OptionListBeanAdapter.notifyDataSetChanged();
                        }
                    }
                });
            }
        };

        OptionListBeanAdapter.addData(specialSingleTopicBean.getOptionList());
        recyclerView.setAdapter(OptionListBeanAdapter);
    }
    //多选题
    private void multiSelect(SpecialBean.AnswerListBean itemsBean, TextView answer,
                             LinearLayout the_analysis_of, TextView more_determine, SpecialSingleTopicBean special,
                             RecyclerView recyclerView,TextView check_the_parsing) {
        final int[] choose = {0};
        OptionListBeanAdapter = new BaseQuickAdapter<SpecialSingleTopicBean.OptionListBean, BaseViewHolder>(R.layout.choose) {
            @Override
            protected void convert(@NotNull BaseViewHolder baseViewHolder, SpecialSingleTopicBean.OptionListBean questionOptionListBean) {
                TextView topic_selection = baseViewHolder.findView(R.id.topic_selection);
                ImageView selected = baseViewHolder.findView(R.id.selected);
                TextView the_order = baseViewHolder.findView(R.id.the_order);
                final LinearLayout linearLayout = baseViewHolder.findView(R.id.the_whole_article);
                the_order.setText(questionOptionListBean.getSerialNumber()[baseViewHolder.getAdapterPosition()]);//顺序ABCD
                topic_selection.setText(Html.fromHtml(questionOptionListBean.getContent()).toString().trim());
                if (itemsBean.getIsRight() != 0) {
                    for (int i = 0; i < itemsBean.getAnswer().size(); i++) {
                        if (itemsBean.getAnswer().get(i).getId() == questionOptionListBean.getOptionId()) {
                            topic_selection.setTextColor(getResources().getColor(R.color.blue));
                            the_order.setTextColor(getResources().getColor(R.color.blue));
                            linearLayout.setBackground(getContext().getResources().getDrawable(R.drawable.asingleframe));
                            selected.setVisibility(View.VISIBLE);
                            refer_to_the_answer = refer_to_the_answer + serialNumber[baseViewHolder.getAdapterPosition()];
                            answer.setText(refer_to_the_answer);
                        }
                    }
                    the_analysis_of.setVisibility(View.VISIBLE);
                } else {
                    topic_selection.setTextColor(getResources().getColor(R.color.colorBlack));
                    the_order.setTextColor(getResources().getColor(R.color.colorBlack));
                    linearLayout.setBackground(getContext().getResources().getDrawable(R.drawable.grey_circle));
                    selected.setVisibility(View.INVISIBLE);
                }

                if (choose[0] == 2 || itemsBean.getIsRight() == 0) {
                    check_the_parsing.setVisibility(View.VISIBLE);
                    the_analysis_of.setVisibility(View.GONE);
                } else {
                    the_analysis_of.setVisibility(View.VISIBLE);
                    check_the_parsing.setVisibility(View.GONE);
                        answerToSubmit(itemsBean);

                }
                check_the_parsing.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if (itemsBean.getIsRight() != 0) {
                            choose[0]=0;
                            the_analysis_of.setVisibility(View.VISIBLE);
                            check_the_parsing.setVisibility(View.GONE);
                        }
                    }
                });
                linearLayout.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if (check_the_parsing.getVisibility()==View.VISIBLE) {
                            choose[0] = 2;
                            if (questionOptionListBean.getStatusValue() == 0) {
                                questionOptionListBean.setStatusValue(1);
                            } else {
                                questionOptionListBean.setStatusValue(0);
                            }

                            switch (questionOptionListBean.getStatusValue()) {
                                case 0:
                                    topic_selection.setTextColor(getResources().getColor(R.color.colorBlack));
                                    the_order.setTextColor(getResources().getColor(R.color.colorBlack));
                                    linearLayout.setBackground(getContext().getResources().getDrawable(R.drawable.grey_circle));
                                    selected.setVisibility(View.INVISIBLE);
                                    break;
                                case 1:
                                    topic_selection.setTextColor(getResources().getColor(R.color.blue));
                                    the_order.setTextColor(getResources().getColor(R.color.blue));
                                    linearLayout.setBackground(getContext().getResources().getDrawable(R.drawable.asingleframe));
                                    selected.setVisibility(View.VISIBLE);
                                    answer.setText(serialNumber[baseViewHolder.getAdapterPosition()]);
                                    break;

                            }
                            List<AnswerBean> answerBeanList = new ArrayList<>();
                            String results = "";
                            String reference = "";
                            for (int i = 0; i < special.getOptionList().size(); i++) {
                                if (special.getOptionList().get(i).getStatusValue() == 1) {
                                    results = results + serialNumber[i];
                                    AnswerBean answerBean = new AnswerBean();
                                    answerBean.setContent(special.getOptionList().get(i).getContent());
                                    answerBean.setId(special.getOptionList().get(i).getOptionId());
                                    answerBeanList.add(answerBean);
                                }
                                if (special.getOptionList().get(i).getIsRightAnswer() == 1) {
                                    reference = reference + serialNumber[i];
                                }
                            }
                            Log.e("数据来源",answerBeanList.size()+"");

                            itemsBean.setAnswer(answerBeanList);
                            answer.setText(results);
                            if (results.equals(reference)) {
                                itemsBean.setIsRight(1);
                                answer.setTextColor(getResources().getColor(R.color.blue));
                            } else {
                                itemsBean.setIsRight(2);
                                answer.setTextColor(getResources().getColor(R.color.red_ff4500));
                            }

                        }else {
                            showBindToast("请选择答案");
                        }
                    }
                });

            }
        };
        OptionListBeanAdapter.addData(specialSingleTopicBean.getOptionList());
        recyclerView.setAdapter(OptionListBeanAdapter);
    }

    //填空
    private void fillsUpTheTopic(SpecialBean.AnswerListBean itemsBean,
                                 TextView check_the_parsing, LinearLayout the_analysis_of,
                                 SpecialSingleTopicBean special, TextView answer, TextView correct_answer) {

        final int[] choose = {0};
        OptionListBeanAdapter = new BaseQuickAdapter<SpecialSingleTopicBean.OptionListBean, BaseViewHolder>(R.layout.fill_in_the_empty_layout) {
            @Override
            protected void convert(@NotNull BaseViewHolder baseViewHolder, SpecialSingleTopicBean.OptionListBean optionListBean) {
                TextView the_order = baseViewHolder.findView(R.id.the_order);
                EditText topic_selection = baseViewHolder.findView(R.id.topic_selection);
                the_order.setText(theSerialNumber[baseViewHolder.getAdapterPosition()]);
                if (itemsBean.getIsRight() == 0) {
                    topic_selection.setFocusable(true);
                } else {
                    topic_selection.setFocusable(false);
                    topic_selection.setText(itemsBean.getAnswer().get(baseViewHolder.getAdapterPosition()).getContent());
                    String results = "";
                    for (int i = 0; i < itemsBean.getAnswer().size(); i++) {
                        if (special.getOptionList().get(i).getIsRightAnswer() == 1) {
                            results = results + theSerialNumber[i] + itemsBean.getAnswer().get(i).getContent();

                        }

                    }
                    if (itemsBean.getIsRight() == 1) {
                        answer.setTextColor(getResources().getColor(R.color.blue));
                    } else {
                        answer.setTextColor(getResources().getColor(R.color.red_ff4500));
                    }
                    answer.setText(results);
                }

                if (choose[0] == 2 || itemsBean.getIsRight() == 0) {
                    check_the_parsing.setVisibility(View.VISIBLE);
                    the_analysis_of.setVisibility(View.GONE);
                    topic_selection.setFocusable(true);
                } else {
                    topic_selection.setFocusable(false);
                    answerToSubmit(itemsBean);
                    the_analysis_of.setVisibility(View.VISIBLE);
                    check_the_parsing.setVisibility(View.GONE);
                }
                topic_selection.addTextChangedListener(new TextWatcher() {
                    @Override
                    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                    }

                    @Override
                    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                    }

                    @Override
                    public void afterTextChanged(Editable editable) {
                        optionListBean.setFillInTheData(editable.toString().trim());
                        choose[0]=2;
                        String results = "";
                        String reference = "";
                        List<AnswerBean> answerBeanList = new ArrayList<>();
                        for (int i = 0; i < special.getOptionList().size(); i++) {
                            if (special.getOptionList().get(i).getIsRightAnswer() == 1) {
                                results = results + theSerialNumber[i] + special.getOptionList().get(i).getFillInTheData();
                                AnswerBean answerBean = new AnswerBean();
                                answerBean.setContent(special.getOptionList().get(i).getFillInTheData());
                                answerBean.setId(special.getOptionList().get(i).getOptionId());
                                answerBeanList.add(answerBean);
                            }
                            if (special.getOptionList().get(i).getIsRightAnswer() == 1) {
                                reference = reference + serialNumber[i];
                            }
                        }
                        if (results.equals(correct_answer.getText().toString())) {
                            answer.setTextColor(getResources().getColor(R.color.blue));
                            itemsBean.setIsRight(1);
                        } else {
                            answer.setTextColor(getResources().getColor(R.color.red_ff4500));
                            itemsBean.setIsRight(2);
                        }
                        itemsBean.setAnswer(answerBeanList);
                        answer.setText(results);
                    }
                });
                check_the_parsing.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if (itemsBean.getIsRight() != 0) {
                            choose[0]=0;


                        }else {
                            showBindToast("请选择答案");
                        }
                    }
                });
            }
        };
    }

    //简答
    private void shortAnswer(EditText short_answer_text, SpecialBean.AnswerListBean itemsBean,
                             TextView check_the_parsing, LinearLayout the_analysis_of, TextView answer,
                             TextView correct_answer, SpecialSingleTopicBean specialSingleTopicBean) {
        answer.setTextColor(getResources().getColor(R.color.blue));
        correct_answer.setText(Html.fromHtml(specialSingleTopicBean.getOptionList().get(0).getContent() ).toString().trim());
        if (itemsBean.getIsRight() == 1) {
            if (itemsBean.getAnswer() != null) {
                if (itemsBean.getAnswer().size() > 0) {
                    short_answer_text.setText(itemsBean.getAnswer().get(0).getContent());
                    answer.setText(itemsBean.getAnswer().get(0).getContent());
                }
            }

            short_answer_text.setFocusable(false);
            check_the_parsing.setVisibility(View.GONE);
            the_analysis_of.setVisibility(View.VISIBLE);
            answerToSubmit(itemsBean);
        } else {
            short_answer_text.setFocusable(true);
            check_the_parsing.setVisibility(View.VISIBLE);
            the_analysis_of.setVisibility(View.GONE);
        }
        short_answer_text.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {

                mShortanSwerBeanList = new ArrayList<>();
                AnswerBean answerBean = new AnswerBean();
                answerBean.setContent(editable.toString());
                mShortanSwerBeanList.add(answerBean);
                itemsBean.setAnswer(mShortanSwerBeanList);
                itemsBean.setIsRight(1);
                answer.setText(editable.toString());

            }
        });
        check_the_parsing.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (itemsBean.getIsRight() != 0) {
                    if (itemsBean.getIsRight() == 1) {
                        if (itemsBean.getAnswer() != null) {
                            if (itemsBean.getAnswer().size() > 0) {
                                short_answer_text.setText(itemsBean.getAnswer().get(0).getContent());
                                answer.setText(itemsBean.getAnswer().get(0).getContent());
                            }
                        }
                        short_answer_text.setFocusable(false);
                        check_the_parsing.setVisibility(View.GONE);
                        the_analysis_of.setVisibility(View.VISIBLE);
                        answerToSubmit(itemsBean);
                    } else {
                        short_answer_text.setFocusable(true);
                        check_the_parsing.setVisibility(View.VISIBLE);
                        the_analysis_of.setVisibility(View.GONE);
                    }
                }else {
                    showBindToast("请选择答案");
                }
            }
        });

    }

    // WebView 权限
    public void jsSupport(WebView webView, String data) {

        WebSettings webSettings = webView.getSettings();

        webSettings.setUseWideViewPort(true);
        webSettings.setLoadWithOverviewMode(true);
        webSettings.setUseWideViewPort(true);  //将图片调整到适合webview的大小
        webSettings.setTextZoom(270);//设置H5页面中字体大小不跟随手机系统

//        if (Build.VERSION.SDK_INT >= 19) {
//            //webSettings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);//加载缓存否则网络
//            webSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);//不加载缓存
//        }
//
//        if (Build.VERSION.SDK_INT >= 19) {
//            webSettings.setLoadsImagesAutomatically(true);//图片自动缩放 打开
//        } else {
//            webSettings.setLoadsImagesAutomatically(false);//图片自动缩放 关闭
//        }
//
//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
//            webView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);//软件解码
//        }
//        webView.setLayerType(View.LAYER_TYPE_HARDWARE, null);//硬件解码
//
//        webSettings.setJavaScriptEnabled(true); // 设置支持javascript脚本
//        webSettings.setSupportZoom(true);// 设置可以支持缩放
//        webSettings.setBuiltInZoomControls(true);// 设置出现缩放工具 是否使用WebView内置的缩放组件，由浮动在窗口上的缩放控制和手势缩放控制组成，默认false
//
//        webSettings.setDisplayZoomControls(false);//隐藏缩放工具
//        webSettings.setUseWideViewPort(true);// 扩大比例的缩放
//
//        webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);//自适应屏幕
//        webSettings.setLoadWithOverviewMode(true);
//
//        webSettings.setDatabaseEnabled(true);//
//        webSettings.setSavePassword(true);//保存密码
//        webSettings.setDomStorageEnabled(true);//是否开启本地DOM存储  鉴于它的安全特性（任何人都能读取到它，尽管有相应的限制，将敏感数据存储在这里依然不是明智之举），Android 默认是关闭该功能的。
//        webView.setSaveEnabled(true);
//        webView.setKeepScreenOn(true);


        webView.setBackgroundColor(0);
//        ImgStr.getWebview(webView,this,ImgStr.getImgStr(data));
        webView.loadDataWithBaseURL(null,  ImgStr.getImgStr(data), "text/html", "utf-8", null);



    }

    @Override
    public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
    }

    public void answerToSubmit(SpecialBean.AnswerListBean itemsBean) {
        SpecialSubmitted mSpecialSubmitted = new SpecialSubmitted();
        SpecialSubmitted.QuestionJsonBean questionJsonBean = new SpecialSubmitted.QuestionJsonBean();
        questionJsonBean.setId(itemsBean.getId());//题id
        questionJsonBean.setIsRight(itemsBean.getIsRight());//对错
        questionJsonBean.setAnswer(itemsBean.getAnswer());
        mSpecialSubmitted.setLibId(mId);//题库ID
        mSpecialSubmitted.setQuestionJson(questionJsonBean);
        Gson gson = new Gson();
        String json1 = gson.toJson(mSpecialSubmitted);
        RequestBody body = RequestBody.create(MediaType.parse("application/json"), json1);
        addDisposable(RetrofitService.getNetService()
                .getSpecialSubmitted(body)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(response -> {
                    if (response.getStatusCode() == Constant.CODE_SUCCESS) {
                    }
                }, throwable -> {
                    Tips.RequestError(getActivity());
                    throwable.printStackTrace();
                }));
    }


}
